<?php
namespace app\common\model;

use app\extra\BaseModel;
use app\extra\DBCont;

class Config extends BaseModel
{
    use \think\model\concern\SoftDelete;
    protected $deleteTime = 'delete_time';

    /**
     * 返回key-value的值
     * @param string $type
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getConfigList($type='key-value'){
        $data_list = $this->order('sort sac')->select();
        $data = [];
        if ($type == 'key-value'){
            foreach ($data_list as $item){
                if ($item['type'] == 'json'){
                    $data[$item['name']] = unSerializeByJson($item['value']);
                }elseif($item['type'] == 'picture'){
                    $data[$item['name']] = [
                      'key' => $item['value'],
                      'value' => showFileUrl($item['value']),
                    ];
                }
            }
        } else {
            foreach ($data_list as &$item){
                if ($item['type'] == 'json'){
                    $item['value'] = unSerializeByJson($item['value']);
                }elseif($item['type'] == 'picture'){
                    $item['value'] = showFileUrl($item['value']);
                }
            }
            $data = $data_list;
        }
        return $data;
    }
}